package cn.edu.neu.studentmsg_java.service.impl;

import cn.edu.neu.studentmsg_java.dao.ClassesDao;
import cn.edu.neu.studentmsg_java.dao.MCDao;
import cn.edu.neu.studentmsg_java.dao.SelectionDao;
import cn.edu.neu.studentmsg_java.dao.StudentDao;
import cn.edu.neu.studentmsg_java.model.Student;
import cn.edu.neu.studentmsg_java.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentDao studentDao;
    @Autowired
    private ClassesDao classesDao;
    @Autowired
    private MCDao mcdao;
    @Autowired
    private SelectionDao selectionDao;

    @Override
    public List<Student> findStudentPart(Student student) {
        return studentDao.selectStudentPart(student);
    }

    @Override
    public int removeStudentById(Integer student_id) {
        //需要先删除选课表中的信息
        selectionDao.deleteSelectionByStudentId(student_id);
        return studentDao.deleteStudentById(student_id);
    }

    //添加学生需要将主修课程信息获取到并绑定在选课表中
    @Override
    public int addStudent(Student student) {
        int result = studentDao.insertStudent(student);
        if (result > 0) {
            //通过班id获取专业id
            int major_id = classesDao.selectMajorIdByClassId(student.getClass_id());
            //通过专业id查询课程id
            List<Integer> courseList = mcdao.selectCourseIdByMajorId(major_id);
            //如果专业id和课程集合不为空
            if (major_id != 0 && courseList != null) {
                //通过学生id信息
                int student_id =  studentDao.selectStudentIdByStudentPart(student);
                //循环变量课程集合
                for (Integer course_id : courseList) {
                    //建立学生和课程集合之间的关联
                    selectionDao.insertSelectionByStudentId(student_id, course_id);
                }
                return 1;
            }
        }
        return 0;
    }

    @Override
    public int modifyStudentById(Student student) {
        return studentDao.updateStudentById(student);
    }

    @Override
    public Student findStudentByStudentId(Integer student_id) {
        return studentDao.selectStudentByStudentId(student_id);
    }

    @Override
    public String findStudentNameByStudentId(Integer student_id) {
        return studentDao.selectStudentNameByStudentId(student_id);
    }
}
